package com.miaohy.demo.controller;


import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.miaohy.demo.entity.Test;
import com.miaohy.demo.mapper.TestMapper;
import com.miaohy.demo.models.DownLoadFIleVO;
import com.miaohy.demo.service.IBasicSeqService;
import com.miaohy.demo.service.IBasicUploadService;
import com.miaohy.demo.service.impl.TestService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2025-02-23
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    IBasicSeqService basicSeqService;

    @Autowired
    IBasicUploadService basicUploadService;

    @Autowired
    TestMapper testMapper;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    TestService testService;

    @GetMapping("/cache")
    R cache(String name) {
        Cache cache = cacheManager.getCache(name);
        com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache =
                (com.github.benmanes.caffeine.cache.Cache<Object, Object>) cache.getNativeCache();

        return R.ok(nativeCache.asMap());
    }

    @GetMapping("/add")
    R add(String name) {
        Test test = new Test();
        test.setName(name);
        testMapper.insert(test);
        Cache cache = cacheManager.getCache("users");
        cache.put(test.getId(), test);
        return R.ok(null);
    }

    @GetMapping("/get")
    R getName(Integer id) {
        return R.ok(testService.getTest(id));

    }

    @GetMapping("/getSeqNumber")
    R<String> getSeqNumber(String seqCode) {
        return R.ok(basicSeqService.getSeqNumber(seqCode));
    }

    @GetMapping("/getSeqNumberParams")
    R<String> getSeqNumberParams(String seqCode, String bu) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("BU", bu);
        return R.ok(basicSeqService.getSeqNumberParams(seqCode, params));
    }

    @GetMapping("/getSeqNumbers")
    R<List<String>> getSeqNumber(String seqCode, Integer num) {
        return R.ok(basicSeqService.getSeqNumbers(seqCode, num));
    }

    @GetMapping("/getSeqNumbersParams")
    R<List<String >> getSeqNumberParams(String seqCode, String bu, Integer num) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("BU", bu);
        return R.ok(basicSeqService.getSeqNumbersParams(seqCode, num, params));
    }

    @Data
    class UploadVO {
        MultipartFile file;
    }

    @PostMapping("/upload")
    R upload(UploadVO vo) {
        basicUploadService.upload(vo.getFile());
        return R.ok("OK");
    }

    @GetMapping("/download")
    void download(DownLoadFIleVO vo, HttpServletResponse response) {
        basicUploadService.download(vo, response);
    }

}

